package com.zhangxin.threadcoreknowledge.threadobjectclasscommonmethods;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  演示: sleep()不会释放Lock(lock需要手动释放)
 */
public class Demo08SleepDontReleaseLock implements Runnable{

    private static final Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        Demo08SleepDontReleaseLock sleepDontReleaseLock = new Demo08SleepDontReleaseLock();
        new Thread(sleepDontReleaseLock).start();
        new Thread(sleepDontReleaseLock).start();
    }

    @Override
    public void run() {
        lock.lock();
        System.out.println("Thread:" + Thread.currentThread().getName() + " got lock");
        try {
            Thread.sleep(5000);
            System.out.println("Thread:" + Thread.currentThread().getName() + " wake up");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock(); // lock 必须要手动解锁
        }
    }
}
